package com.ruoyi.business.service.impl;

import com.ruoyi.business.domain.TRecipientInfo;
import com.ruoyi.business.domain.TServerMerInfo;
import com.ruoyi.business.mapper.TRecipientInfoMapper;
import com.ruoyi.business.service.ITPayChannelService;
import com.ruoyi.business.service.ITRecipientInfoService;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysUserRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 收款账号Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-02-26
 */
@Service
public class TRecipientInfoServiceImpl implements ITRecipientInfoService 
{
    @Autowired
    private TRecipientInfoMapper tRecipientInfoMapper;

    @Autowired
    private ITPayChannelService payChannelService;

    /**
     * 查询收款账号
     * 
     * @param id 收款账号主键
     * @return 收款账号
     */
    @Override
    public TRecipientInfo selectTRecipientInfoById(Long id)
    {
        return tRecipientInfoMapper.selectTRecipientInfoById(id);
    }

    /**
     * 查询收款账号列表
     * 
     * @param tRecipientInfo 收款账号
     * @return 收款账号
     */
    @Override
    @DataScope(userAlias = "t")
    public List<TRecipientInfo> selectTRecipientInfoList(TRecipientInfo tRecipientInfo)
    {
        return tRecipientInfoMapper.selectTRecipientInfoList(tRecipientInfo);
    }

    @Override
    public List<TRecipientInfo> selectTRecipientInfoListByNoAuth(TRecipientInfo tRecipientInfo) {
        return tRecipientInfoMapper.selectTRecipientInfoListByNoAuth(tRecipientInfo);
    }

    /**
     * 新增收款账号
     * 
     * @param tRecipientInfo 收款账号
     * @return 结果
     */
    @Override
    public int insertTRecipientInfo(TRecipientInfo tRecipientInfo)
    {
        return tRecipientInfoMapper.insertTRecipientInfo(tRecipientInfo);
    }

    /**
     * 修改收款账号
     * 
     * @param tRecipientInfo 收款账号
     * @return 结果
     */
    @Override
    public int updateTRecipientInfo(TRecipientInfo tRecipientInfo)
    {
        return tRecipientInfoMapper.updateTRecipientInfo(tRecipientInfo);
    }

    /**
     * 批量删除收款账号
     * 
     * @param ids 需要删除的收款账号主键
     * @return 结果
     */
    @Override
    public int deleteTRecipientInfoByIds(String ids)
    {
        return tRecipientInfoMapper.deleteTRecipientInfoByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除收款账号信息
     * 
     * @param id 收款账号主键
     * @return 结果
     */
    @Override
    public int deleteTRecipientInfoById(Long id)
    {
        return tRecipientInfoMapper.deleteTRecipientInfoById(id);
    }

    @Override
    public List<TRecipientInfo> getAvailableListByMoney(long userId, TServerMerInfo tServerMerInfo, long money, String channel) {
        TRecipientInfo query = new TRecipientInfo();
        query.setAgentUid(userId);
        query.setMoney(money);
        query.setChannel(channel);
        if(tServerMerInfo!=null){
            query.setUserId(tServerMerInfo.getUserId());
        }
        return tRecipientInfoMapper.getAvailableListByMoney(query);
    }

    @Override
    public List<TRecipientInfo> getAvailableListBySupportMoney(long userId, TServerMerInfo tServerMerInfo,long money, String channel) {
        TRecipientInfo query = new TRecipientInfo();
        query.setAgentUid(userId);
        query.setMoney(money);
        query.setChannel(channel);
        if(tServerMerInfo!=null){
            query.setUserId(tServerMerInfo.getUserId());
        }
        return tRecipientInfoMapper.getAvailableListBySupportMoney(query);
    }

    @Override
    public List<TRecipientInfo> getAvailableListNew(TRecipientInfo recipientInfoParam) {

        return tRecipientInfoMapper.getAvailableListNew(recipientInfoParam);
    }

    @Override
    public List<TRecipientInfo> getAvailableList(long userId, String channelType, String channel) {
        TRecipientInfo query = new TRecipientInfo();
        query.setUserId(userId);
        query.setChannelType(channelType);
        query.setChannel(channel);
        return tRecipientInfoMapper.getAvailableList(query);
    }

    @Override
    public List<TRecipientInfo> getRecipientInfo(TRecipientInfo recipientInfo) {

        return tRecipientInfoMapper.getRecipientInfo(recipientInfo);
    }

    @Override
    public void resetTRecipientInfo() {
        tRecipientInfoMapper.resetTRecipientInfo();
    }

    @Override
    public List<TRecipientInfo> selectTRecipientGroupNameList(TRecipientInfo recipientInfo) {
        return tRecipientInfoMapper.selectTRecipientGroupNameList(recipientInfo);
    }

    @Override
    public void serviceRecip(Long userId, Long[] ids) {
        if (StringUtils.isNotNull(ids))
        {
            // 新增用户与角色管理
            List<TRecipientInfo> list = new ArrayList<TRecipientInfo>();
            for (Long id : ids)
            {
                TRecipientInfo ur = new TRecipientInfo();
                ur.setId(id);
                ur.setServeMerId(userId);
                tRecipientInfoMapper.updateTRecipientInfo(ur);
            }

        }
    }

}
